package KmlParser;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadKMLFile {
	Document kmlFile;

	public ReadKMLFile(String file) {

		try {

			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			kmlFile = dBuilder
					.parse(file);
			kmlFile.getDocumentElement().normalize();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String[] getValuesByTag(String tagParent, String tagChild) {
		String[] valuesByTag;
		NodeList nList = kmlFile.getElementsByTagName(tagParent);
		valuesByTag = new String[nList.getLength()];

		for (int temp = 0; temp < nList.getLength(); temp++) {

			Node nNode = nList.item(temp);
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {

				Element eElement = (Element) nNode;

				valuesByTag[temp] = new String(getTagValue(tagChild, eElement));
			}
		}
		
		return valuesByTag;
	}

	private static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
				.getChildNodes();

		Node nValue = (Node) nlList.item(0);
		return nValue.getNodeValue();
	}
}